// created by noellynch
// May 14, 2011

package com.gruntdb.core;

import java.io.File;


import com.gruntdb.core.stubs.IStorageEngine;
import com.gruntdb.engines.indexedjson.IndexedJSONStoreManager;
import com.gruntdb.engines.indexedjson.IndexedJSONStorageIndex;
import com.gruntdb.engines.indexedjson.IndexedJSONStorageProperties;
import com.gruntdb.objects.GruntDBIndexIterator;
import com.gruntdb.objects.GruntDBIndexStore;
import com.gson4javaext.core.GSON4JavaExt;

public class GruntDataStore<E> {
	private		String		name;
	private		String		location;
	private		String		valueClassName;
	private		Class		valueClass;
	
	private		IStorageEngine		mcl_storageEngine;
	
	private	transient	File		mcl_location;
	
	protected GruntDataStore() {
		super();
	}

	protected GruntDataStore(String name, String location, Class valueClass, Class<? extends IStorageEngine> cl_storageEngineClass) {
		this();
		
		this.name = name;
		this.location = location;
		this.valueClass = valueClass;
		
		mcl_location = new File(location);
		if(!mcl_location.exists()) {
			mcl_location.mkdir();
		}
		
		valueClassName = valueClass.getName().replace(".", "_");
		
		try {
			System.out.println("Storage Engine is of type " + cl_storageEngineClass.getName());
			mcl_storageEngine = cl_storageEngineClass.newInstance();
			mcl_storageEngine.initialise(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public	GruntDBIndexIterator	getIndexIterator() {
		return mcl_storageEngine.getIndexIterator();
	}
	
	public	long	add(E cl_value) {
		return mcl_storageEngine.add(cl_value);
	}
	
	public	void	close() {
		mcl_storageEngine.close();
	}
	
	public	final	String		getFileReferenceName() {
		return name + "_" + valueClassName;
	}

	public String getName() {
		return name;
	}

	public String getLocation() {
		return location;
	}	
}
